package top.anonyeast.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

//时间处理器，需要实现MetaObjectHandler接口来实现自动填充
@Component
public class TimeHandler implements MetaObjectHandler {
    private final Date DATA_TIME = new Date();

    //插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        //setFieldValByName(bean的属性名,填充的值，metaObject)
        this.setFieldValByName("createTime",DATA_TIME,metaObject);
        this.setFieldValByName("updateTime",DATA_TIME,metaObject);
    }

    //更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime",DATA_TIME,metaObject);
    }
}
